package com.xly.webclick.databinding;

import android.databinding.BindingAdapter;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xly.webclick.utils.DisplayUtils;


/**
 * Created by amos on 2018/6/4.
 */

public class ViewBindingAdapter {

    @BindingAdapter("android:layout_marginTop")
    public final static void setLayoutMarginTop(View view, int size) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            if (lp instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) lp).setMargins(
                        ((ViewGroup.MarginLayoutParams) lp).leftMargin
                        , size
                        , ((ViewGroup.MarginLayoutParams) lp).rightMargin
                        , ((ViewGroup.MarginLayoutParams) lp).bottomMargin);
                view.setLayoutParams(lp);
            }
        }
    }

    @BindingAdapter({"recyAdapter"})
    public static void setAdapter(RecyclerView view, RecyclerView.Adapter adapter) {
        if (adapter != null) {
            view.setLayoutManager(new LinearLayoutManager(view.getContext()));
            view.setAdapter(adapter);
        }
    }

    @BindingAdapter("android:layout_marginBottom")
    public final static void setLayoutMarginBottom(View view, int size) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            if (lp instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) lp).setMargins(
                        ((ViewGroup.MarginLayoutParams) lp).leftMargin
                        , ((ViewGroup.MarginLayoutParams) lp).topMargin
                        , ((ViewGroup.MarginLayoutParams) lp).rightMargin
                        , size);
                view.setLayoutParams(lp);
            }
        }
    }


    @BindingAdapter("android:layout_marginLeft")
    public final static void setLayoutMarginLeft(View view, int size) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            if (lp instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) lp).setMargins(
                        size
                        , ((ViewGroup.MarginLayoutParams) lp).topMargin
                        , ((ViewGroup.MarginLayoutParams) lp).rightMargin
                        , ((ViewGroup.MarginLayoutParams) lp).bottomMargin);
                view.setLayoutParams(lp);
            }
        }
    }
    @BindingAdapter("android:layout_marginRight")
    public final static void setLayoutMarginRight(View view, int size) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            if (lp instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) lp).setMargins(((ViewGroup.MarginLayoutParams) lp).leftMargin
                        , ((ViewGroup.MarginLayoutParams) lp).topMargin
                        , size
                        , ((ViewGroup.MarginLayoutParams) lp).bottomMargin);
                view.setLayoutParams(lp);
            }
        }
    }


    @BindingAdapter("android:layout_marginStart")
    public final static void setLayoutMarginStart(View view, int size) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            if (lp instanceof ViewGroup.MarginLayoutParams) {
                ((ViewGroup.MarginLayoutParams) lp).setMarginStart(size);
                view.setLayoutParams(lp);
            }
        }
    }


    @BindingAdapter("view_width")
    public final static void setLayoutWidth(View view, int width) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            lp.width = width;
            view.setLayoutParams(lp);
        }
    }

    @BindingAdapter("view_height")
    public final static void setLayoutHeight(View view, int height) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp != null) {
            lp.height = height;
            view.setLayoutParams(lp);
        }
    }

    @BindingAdapter("enableAdjustResize")
    public final static void setAdjust(View view, boolean adjust) {
        if (adjust) {
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp != null) {
                int pl = DisplayUtils.getSize(view.getPaddingLeft());
                int pt = DisplayUtils.getSize(view.getPaddingTop());
                int pr = DisplayUtils.getSize(view.getPaddingRight());
                int pb = DisplayUtils.getSize(view.getPaddingBottom());


                int ps = DisplayUtils.getSize(view.getPaddingStart());
                int pe = DisplayUtils.getSize(view.getPaddingEnd());

                if (view.isPaddingRelative()) {
                    view.setPaddingRelative(ps, pt, pe, pb);
                }
                else {
                    view.setPadding(pl, pt, pr, pb);
                }

                int w = lp.width;
                int h = lp.height;

                if (w > 0) {
                    lp.width = DisplayUtils.getSize(w);
                }

                if (h > 0) {
                    lp.height = DisplayUtils.getSize(h);
                }

                int minWidth = view.getMinimumWidth();
                int minHeight = view.getMinimumHeight();

                if (minWidth > 0) {
                    view.setMinimumWidth(DisplayUtils.getSize(minWidth));
                }

                if (minHeight > 0) {
                    view.setMinimumHeight(DisplayUtils.getSize(minHeight));
                }

                if (lp instanceof ViewGroup.MarginLayoutParams) {
                    int ml = DisplayUtils.getSize(((ViewGroup.MarginLayoutParams) lp).leftMargin);
                    int mt = DisplayUtils.getSize(((ViewGroup.MarginLayoutParams) lp).topMargin);
                    int mr = DisplayUtils.getSize(((ViewGroup.MarginLayoutParams) lp).rightMargin);
                    int mb = DisplayUtils.getSize(((ViewGroup.MarginLayoutParams) lp).bottomMargin);

                    int ms = DisplayUtils.getSize(((ViewGroup.MarginLayoutParams) lp).getMarginStart());
                    int me = DisplayUtils.getSize(((ViewGroup.MarginLayoutParams) lp).getMarginEnd());

                    ((ViewGroup.MarginLayoutParams) lp).setMargins(ml
                            , mt
                            , mr
                            , mb);

                    if (((ViewGroup.MarginLayoutParams) lp).isMarginRelative()) {
                        ((ViewGroup.MarginLayoutParams) lp).setMarginStart(ms);
                        ((ViewGroup.MarginLayoutParams) lp).setMarginEnd(me);
                    }
                }

                view.setLayoutParams(lp);


                if (view instanceof TextView) {
                    float tz = ((TextView) view).getTextSize();
                    int tmw = ((TextView) view).getMaxWidth();

                    ((TextView) view).setMaxWidth(DisplayUtils.getSize(tmw));
                    ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,DisplayUtils.getSize((int) tz));
                }

                if (view instanceof CardView) {
                    float radius = ((CardView) view).getRadius();
                    ((CardView) view).setRadius(DisplayUtils.getSize((int) radius));
                }
            }
        }
    }

}
